/*
 *  Copyright 2013 National Institute of Advanced Industrial Science and Technology
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.sss.mapreduce;

import java.io.Serializable;
import java.net.InetAddress;
import java.net.InetSocketAddress;

/**
 * <code>StorageNode</code> represents a storage node in SSS.
 */
@SuppressWarnings("serial")
public class StorageNode implements Serializable {
  private InetSocketAddress address;
  private String protocol;
  private String dbFactory;
  /**
   * Allocates a <code>StorageNode</code> object and initializes it so that
   * it has the given protocol, hostname, port, and DBFactory.
   *
   * @param  protocol  the protocol name this <code>StorageNode</code> supports
   * @param  hostname  ths hostname of this <code>StorageNode</code>
   * @param  port      ths port number of this <code>StorageNode</code> listens to
   * @param  dbf       the name of <code>DBFactorey</code> this <code>StorageNode</code> uses
   * @see    org.sss.mapreduce.DBFactory
   */
  public StorageNode(String protocol, String hostname, int port, String dbf) {
    this.address = new InetSocketAddress(hostname, port);
    this.protocol = protocol;
    this.dbFactory = dbf;
  }
  /**
   * Returns the location of this <code>StorageNode</code> as <code>InetSocketAddress</code>.
   *
   * @return the location of this <code>StorageNode</code> as <code>InetSocketAddress</code>
   * @see    java.net.InetSocketAddress
   */
  public InetSocketAddress getInetSocketAddress() {
    return address;
  }
  /**
   * Returns the location of this <code>StorageNode</code> as <code>InetAddress</code>.
   *
   * @return the location of this <code>StorageNode</code> as <code>InetAddress</code>
   * @see    java.net.InetAddress
   */
  public InetAddress getInetAddress() {
    return address.getAddress();
  }
  /**
   * Returns the fully qualified domain name of this <code>StorageNode</code>.
   *
   * @return the fully qualified domain name of this <code>StorageNode</code>
   */
  public String getCanonicalHostName() {
    return address.getAddress().getCanonicalHostName();
  }
  /**
   * Returns the host name for this <code>StorageNoge</code>.
   *
   * @return the host name for this <code>StorageNode</code>
   */
  public String getHostName() {
    return address.getHostName();
  }
  /**
   * Returns the port number of this <code>StorageNode</code>.
   *
   * @return the port number of this <code>StorageNode</code>
   */
  public int getPort() {
    return address.getPort();
  }
  /**
   * Returns the protocol name this <code>StorageNode</code> supports.
   *
   * @return the protocol name this <code>StorageNode</code> supports
   */
  public String getProtocol() {
    return protocol;
  }
  /**
   * Returns the <code>DBFactory</code> name this <code>StorageNode</code> uses.
   *
   * @return the <code>DBFactory</code> name this <code>StorageNode</code> uses
   * @see    org.sss.mapreduce.DBFactory
   */
  public String getDBFactory() {
    return dbFactory;
  }
  /**
   * Indicates whether some other <code>StorageNode</code> object is "equal to" this one.
   *
   * @param  other  the reference object with which to compare.
   * @return <tt>true</tt> if this object is the same as the <tt>other</tt> argument;
   * <tt>false</tt> otherwise.
   */
  @Override
  public boolean equals(Object other) {
    if (other == null) {
      return false;
    }
    if (this == other) {
      return true;
    }
    if (!(other instanceof StorageNode)) {
      return false;
    }
    StorageNode o = (StorageNode) other;

    return address.equals(o.getInetSocketAddress()) &&
        protocol.equals(o.getProtocol()) &&
        (dbFactory != null?dbFactory.equals(o.dbFactory):o.dbFactory == null);
  }
  /**
   * Returns a hash code value for this <code>StorageNode</code> object.
   *
   * @return a hash code value for this object.
   */
  @Override
  public int hashCode() {
    return address.hashCode() +
           protocol.hashCode() +
           (dbFactory != null?dbFactory:"null").hashCode();
  }
  /**
   * Returns the string representation for this <code>StorageNode</code>
   * <p>
   * A string representation of a <code>StorageNode</code> is in the form:
   * <blockquote><pre>
   *   "protocol://hostname:port/dbFactory"
   * </pre></blockquote>
   *
   * @return the string representation for this <code>StorageNode</code>
   */
  @Override
  public String toString() {
    return protocol + "://" + getHostName() + ":" + getPort() + "/" + (dbFactory != null?dbFactory:"");
  }
}
